#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   GC.
@Time    :   2023/03/17 22:00:51
@author  :   Eval
@Version :   1.0
@Describe:   全局游戏静态管理器
'''
import pygame
from json import loads as JsonLoads
from conf.GameLogin import *

class GC:
    """全局游戏管理"""
    # 资源注册文件
    GC_SourceMgr:dict = None
    # 加载全局脚本配置
    GC_GlobalScripts:dict = None
    GC_MapImages:list[dict] = None
    # 获取所有的地图名称
    GC_MapNameList = None
    GC_LevelConf = None
    GC_Hero = None
    GC_IType = None

    # 全局游戏资源缓存字典,如果缓存字典里有这个图块的surface缓存,那么直接从缓存里面拿出来.避免多次执行操作影响性能
    # 编辑器格式-->key : 图块类型-当前索引  or 图块类型-x-y
    GC_GameSurfaceCache:dict[pygame.Surface] = {}

    # 缓存全局游戏事件的变量
    EV_eventFlags:dict[object] = {}

    # 将所有脚本按照编号的形式存储
    GC_Scripts = {}
    # 存储当前楼层的所有怪物信息
    GC_FloorMonsterData:dict = {}

    @staticmethod
    def _loadGC():
        GC.GC_SourceMgr= GC.__loadFile("Source")
        # 加载全局脚本配置
        GC.GC_GlobalScripts = GC.__loadFile("GlobalScript")

        GC.GC_MapImages = GC.GC_GlobalScripts["MapImages"]
        # 获取所有的地图名称
        GC.GC_MapNameList = GC.GC_GlobalScripts["FloorFiles"]
        GC.GC_LevelConf = GC.GC_GlobalScripts["LevelConf"]
        GC.GC_Hero = GC.GC_GlobalScripts["Hero"]

        GC.GC_Scripts = {
            "monsterBook":{
                "name":"怪物手册",
                "function":GC.__getattribute__(GC,"UseMonsterBook")
            },
            "addM":{
                "name":"增加金币",
                "function":GC.__getattribute__(GC,"AddMoney")
            },
            "addExp":{
                "name":"增加经验",
                "function":GC.__getattribute__(GC,"AddExp")
            }
        }
        GC.GC_IType = GC.GC_GlobalScripts["IType"]
    
    @staticmethod
    def __loadFile(fileName:str) -> dict:
        """加载资源文件"""
        data = None
        with open(f"conf/{fileName}.json",encoding="utf8") as f:
            data = JsonLoads(f.read())
        print_info(f"加载游戏脚本:{fileName}.json 完成")
        return data
    
    
    @staticmethod
    def loadSurface(fileName:str,pathArg=None):
        """根据图片名称加载缓存,如果路径参数(pathArg)不为空, 那么根据传进来的路径加载"""
        sourface = GC.GC_GameSurfaceCache.get(fileName)
        path = f"lib/images/{fileName}"
        if pathArg is not None:
            path = pathArg
        if sourface is None:
            sourface = pygame.image.load(f"{path}")
            GC.GC_GameSurfaceCache[fileName] = sourface
        return sourface
    
    @staticmethod
    def getByPosSource(IKey:str):
        """根据传进来的索引返回当前坐标的图块信息"""
        return GC.GC_SourceMgr.get(str(IKey))
    

    @staticmethod
    def useScript(ScriptName:str,args:list=None):
        """ScriptName:脚本名称, args:脚本需要执行的参数,以列表形式传递"""
        script = GC.GC_Scripts.get(ScriptName)
        if script is None:
            print_error(f"无效脚本:{ScriptName},来自道具:{args[1].name} 的调用")
            return
        script["function"](args)

    
    @staticmethod
    def UseMonsterBook(*args):
        """使用怪物手册"""
        from lib.scripts.MonsterHandbook import MonsterHandbook
        from lib.scripts.Package import Package
        MonsterBook:MonsterHandbook = args[0][0].MonsterBook
        bag:Package = args[0][0].GameBag
        bag.ShowUI(False)  # 关闭背包
        MonsterBook.ShowUI()

    @staticmethod
    def AddMoney(*args):
        """主角获得金币"""
        # 使用模块需要单独引用
        from lib.scripts.Hero import Hero
        hero:Hero = args[0][0].hero
        hero.addMoney(args[0][1].value)
    
    @staticmethod
    def AddExp(*args):
        """主角获得经验"""
        from lib.scripts.Hero import Hero
        from lib.scripts.Package import Item
        hero:Hero = args[0][0].hero
        item:Item = args[0][1]
        hero.upgrade(item.value)